package com.charles.seckillsystem.controller;

import com.alipay.api.AlipayApiException;
import com.charles.seckillapi.domain.SysGood;
import com.charles.seckillapi.domain.SysOrder;
import com.charles.seckillcommon.core.constants.CacheConstants;
import com.charles.seckillcommon.redis.service.RedisService;
import com.charles.seckillsystem.mapper.SysGoodMapper;
import com.charles.seckillsystem.service.AlipayOrderService;
import com.charles.seckillsystem.vo.OrderPayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author Charles-H
 * 
 * 支付宝控制层
 */
@RestController
@RequestMapping("/alipay")
@CrossOrigin
public class AliPayController {
    
    @Resource
    private AlipayOrderService alipayOrderService;

    @Resource
    private SysGoodMapper sysGoodMapper;
    
    @Autowired
    private RedisService redisService;

    /**
     * 返回AliPay支付页面
     */
    @PostMapping("/payOrder/{orderId}")
    public String payOrder(@PathVariable("orderId") String orderId) throws AlipayApiException {
        OrderPayVo orderVo = new OrderPayVo();
        SysOrder order = redisService.getCacheObject(CacheConstants.ORDER_ID + orderId);
        SysGood good = sysGoodMapper.getGood(order.getGood_id());
        orderVo.setOut_trade_no(order.getOrder_id());
        orderVo.setSubject(good.getGood_title());
        orderVo.setTotal_amount(good.getSeckill_price().toString());
        orderVo.setBody(good.getGood_desc());
        return alipayOrderService.payOrder(orderVo);
    }
    
}
